package com.yohann.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yohann.commonutils.result.R;
import com.yohann.statistics.client.UcenterClient;
import com.yohann.statistics.entity.Daily;
import com.yohann.statistics.mapper.DailyMapper;
import com.yohann.statistics.service.DailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author Yohann
 * @since 2020-09-02
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {
    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        LambdaQueryWrapper<Daily> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Daily::getDateCalculated, day);
        baseMapper.delete(queryWrapper);

        R r = ucenterClient.countRegister(day);
        Integer count = (Integer)r.getData().get("count");

        Daily daily = new Daily();
        daily.setRegisterNum(count);
        daily.setDateCalculated(day);
        daily.setVideoViewNum(RandomUtils.nextInt(50,100));
        daily.setLoginNum(RandomUtils.nextInt(50,100));
        daily.setCourseNum(RandomUtils.nextInt(50,100));

        baseMapper.insert(daily);
    }

    /**
     * 查询数据
     * @param type
     * @param begin
     * @param end
     * @return
     */
    @Override
    public Map<String, Object> showData(String type, String begin, String end) {
        QueryWrapper<Daily> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("date_calculated",type);
        queryWrapper.between("date_calculated",begin,end);

        List<Daily> list = baseMapper.selectList(queryWrapper);

        List<String> dateList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();

        for (Daily daily : list) {
            dateList.add(daily.getDateCalculated());
            switch (type) {
                case "login_num":
                    numList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numList.add(daily.getCourseNum());
                    break;
                default:break;
            }
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("dateList",dateList);
        map.put("numList",numList);

        return map;
    }
}
